﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.DataAccess;

namespace YiYiTong.Rocket.Business
{
    public class MenuBiz
    {
        private static readonly ModelHandler<MenuInfo> modelHelper = new ModelHandler<MenuInfo>();

        #region Instance
        private MenuBiz()
        {
        }
        private static MenuBiz _instance;

        private static object _syncObject = new object();

        public static MenuBiz Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new MenuBiz();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menuCode">菜单代码</param>
        /// <param name="menuName">菜单名称</param>
        /// <param name="menuUrl">菜单对应地址</param>
        /// <param name="parentId">菜单父编号,0顶级菜单</param>
        /// <param name="isVisible">是否可见 1不可见 0可见</param>
        /// <param name="remark">备注</param>
        /// <returns>返回是否添加成功</returns>
        public int AddMenu(string menuCode, string menuName, string menuUrl, int parentId, int isVisible, string remark, out string strOutPut)
        {
            int ret = 0;
            strOutPut = "添加菜单失败";
            try
            {
                DataTable dtMenu = MenuDataAccess.Instance.GetMenuInfo(menuName);
                if (dtMenu != null && dtMenu.Rows.Count != 0)
                {
                    ret = -1;
                    strOutPut = "菜单名称已存在";
                    return ret;
                }


                string createTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                ret = MenuDataAccess.Instance.AddMenu(menuCode, menuName, menuUrl, parentId, isVisible, createTime, 0, remark);
                if (ret > 0)
                {
                    strOutPut = "添加菜单成功";
                }
            }
            catch
            {
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="menuId">菜单编号</param>
        /// <param name="menuCode">菜单代码</param>
        /// <param name="menuName">菜单名称</param>
        /// <param name="menuUrl">菜单对应地址</param>
        /// <param name="parentId">菜单父编号,0顶级菜单</param>
        /// <param name="isVisible">是否可见 1不可见 0可见</param>
        /// <param name="remark">备注</param>
        /// <returns>返回是否添加成功</returns>
        public int EditMenu(int menuId, string menuCode, string menuName, string menuUrl, int parentId, int isVisible, string remark,  out string strOutPut)
        {
            int ret = 0;
            strOutPut = "修改菜单失败";
            try
            {
                DataTable dtMenu = MenuDataAccess.Instance.GetMenuInfo(menuName);
                if (dtMenu != null && dtMenu.Rows.Count != 0)
                {
                    ret = MenuDataAccess.Instance.EditMenu(menuId, menuCode, menuUrl, parentId, isVisible, remark);
                }
                else
                {
                    ret = MenuDataAccess.Instance.EditMenu(menuId, menuCode, menuName, menuUrl, parentId, isVisible, remark);
                }

                if (ret > 0)
                {
                    strOutPut = "修改菜单成功";
                }
            }
            catch
            {
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 删除菜单（逻辑删除）
        /// </summary>
        /// <param name="menuId">菜单编号</param>
        /// <returns></returns>
        public int DeleteMenu(int menuId, out string strOutPut)
        {
            int ret = 0;
            strOutPut = "删除菜单失败";
            try
            {
                DataTable dtMenu = MenuDataAccess.Instance.GetMenuInfo(menuId);
                if (dtMenu == null && dtMenu.Rows.Count == 0)
                {
                    ret = -1;
                    strOutPut = "删除菜单失败,菜单不存在";
                    return ret;
                }
                else
                {
                    ret = MenuDataAccess.Instance.DeleteMenu(menuId);
                }

                if (ret > 0)
                {
                    strOutPut = "删除菜单成功";
                }
            }
            catch
            {
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 查询菜单
        /// </summary>
        /// <param name="menuCode">菜单代码</param>
        /// <param name="menuName">菜单名称</param>
        /// <param name="isVisible">是否可见 -10 全部 0可见 1不可见</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageRows">每页行数</param>
        /// <returns></returns>
        public DataSet GetPageList(string menuCode, string menuName,  int isVisible, int pageIndex, int pageRows)
        {
            return MenuDataAccess.Instance.GetPageList(menuCode, menuName, isVisible, pageIndex, pageRows);
        }
         
        /// <summary>
        /// 根据菜单编号获取菜单信息
        /// </summary>
        /// <param name="menuId">菜单编号</param>
        /// <returns></returns>
        public MenuInfo GetMenuInfoById(int menuId)
        {
            MenuInfo menu = new MenuInfo();
            DataTable dtMenu = MenuDataAccess.Instance.GetMenuInfo(menuId);
            if (dtMenu != null && dtMenu.Rows.Count != 0)
            {
                menu = modelHelper.FillModel(dtMenu.Rows[0]);
            }
            return menu;
        }

        /// <summary>
        /// 获取所有菜单列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllMenuList()
        {
            return MenuDataAccess.Instance.GetAllMenuList();
        }
    }
}
 